package thread.cas;

import java.util.concurrent.atomic.AtomicReference;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author jiangwz
 * @create 2019/9/13.
 */
@Data
@AllArgsConstructor
class User {
    String username;
    int age;
}

/**
 * 原子引用类 可以自定义自己的原子类 类似于 AtomicInteger
 */
public class AtomicReferenceDemo {

    public static void main(String[] args){
        User z3 = new User("z3", 22);
        User li4 = new User("li4", 24);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(z3);

        System.out.println(atomicReference.compareAndSet(z3, li4) + " " + atomicReference.get());
        System.out.println(atomicReference.compareAndSet(z3, li4) + " " + atomicReference.get());
    }
}
